package com.anquing.socket_netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.UUID;

import static java.lang.Thread.sleep;

/**
 *服务器端自定义Handler
 * @author wangquanqing
 * @since 2021/7/21 9:57 下午
 */
public class MyServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

        //打印出客户端地址
        System.out.println(ctx.channel().remoteAddress()+", "+msg);
        sleep(5000);
        ctx.channel().writeAndFlush("form server: "+ UUID.randomUUID());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}
